﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DispatchAssist.BusinessLogical.DAL;
using CarolLib;
using DispatchAssist.BusinessLogical.Model;
using System.Data;
using System.Configuration;
using System.Xml;

namespace DispatchAssist.BusinessLogical.BLL
{
    public class MsgTemplateManager
    {
        #region Members

        private IDataProvider _provider;
        static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private readonly static string ALlMsgTemplateCacheKey = "Cache_Template";

        public static readonly MsgTemplateManager Instance = new MsgTemplateManager();

        #endregion

        public MsgTemplateManager()
        {
            _provider = new SqlDataProvider();
        }

        #region Method Body

        public List<MsgTemplate> GetAllMsgTemplateRecords()
        {
            try
            {
                return CommonCacheData.CacheTemplate.InitList;
            }
            catch (Exception e)
            {
                Logger.Error("MsgTemplateManager. GetAllMsgTemplateRecords() " + e.Message);
                return null;
            }
        }

        public bool CreateMsgTemplate(MsgTemplate MsgTemplate)
        {
            try
            {
                if (_provider.CreateMsgTemplate(MsgTemplate))
                {
                    CacheHelper.Remove(ALlMsgTemplateCacheKey);
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                Logger.Error("MsgTemplateManager. CreateMsgTemplate() " + e.Message);
                return false;
            }
        }

        public bool UpdateMsgTemplate(MsgTemplate MsgTemplate)
        {
            try
            {
                if (_provider.UpdateMsgTemplate(MsgTemplate))
                {
                    CacheHelper.Remove(ALlMsgTemplateCacheKey);
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                Logger.Error("MsgTemplateManager. UpdateMsgTemplate() " + e.Message);
                return false;
            }
        }

        public bool DeleteMsgTemplate(Guid MsgTemplateID)
        {

            try
            {
                if (_provider.DeleteMsgTemplate(MsgTemplateID))
                {
                    CacheHelper.Remove(ALlMsgTemplateCacheKey);
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                Logger.Error("MsgTemplateManager. DeleteMsgTemplate() " + e.Message);
                return false;
            }
        }

        private XmlNode GetXmlNodeByType(XmlNode parentNode, string type)
        {

            XmlNode xnode = null;
            foreach (XmlNode xmln in parentNode.ChildNodes)
            {
                if (xmln.Attributes["value"].Value == type) { xnode = xmln; break; }

            }
            return xnode;
        }

        public DataSet GetConfigTempInfo(string type)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("key");
            dt.Columns.Add("value");
            dt.Columns.Add("comment");


            XmlNode node = (XmlNode)ConfigurationManager.GetSection("temp");

            XmlNode xmln = GetXmlNodeByType(node, type);
            if (xmln != null)
            {
                dt.Rows.Add(new string[] { "请选择", "-1" });
                foreach (XmlNode xn in xmln.ChildNodes)
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(xn.OuterXml);
                    string keystr = xmlDoc.SelectSingleNode("/itemn/key").InnerText;
                    string valuestr = xmlDoc.SelectSingleNode("/itemn/value").InnerText;
                    string commentstr = xmlDoc.SelectSingleNode("/itemn/comment").InnerText;

                    dt.Rows.Add(new string[] { keystr, valuestr, commentstr });

                }
            }

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            return ds;
        }

        #endregion
    }
}
